<?php

class Custom_Proxy_Executer extends Custom_Proxy_Curl {
	
	private $key = '82693841b58492a8eaef';
	private $port = 7989;
	private $opp_finder_hostname = 'oppfinder.tg.local';
	private $mqueue_hostname = 'oppfinder.tg.local';
	
	private $coupon_creator_hosts = array ('173.203.86.58', '173.203.95.150' );
	private $sf_query_hosts = array ('173.203.86.58', '173.203.95.150' );
	private $si_query_hosts = array ('173.203.86.58', '173.203.95.150' );
	private $sshot_query_hosts = array ('184.106.131.158' );
	
	private $coupon_creator_hostname;
	private $sf_query_hostname;
	private $si_query_hostname;
	private $sshot_query_hostname;
	
	public function __construct() {
		parent::__construct ();
		$rand = mt_rand ( 0, count ( $this->coupon_creator_hosts ) - 1 );
		$this->coupon_creator_hostname = $this->coupon_creator_hosts [$rand];
		
		$rand = mt_rand ( 0, count ( $this->sf_query_hosts ) - 1 );
		$this->sf_query_hostname = $this->sf_query_hosts [$rand];
		
		$rand = mt_rand ( 0, count ( $this->si_query_hosts ) - 1 );
		$this->si_query_hostname = $this->si_query_hosts [$rand];
		
		$rand = mt_rand ( 0, count ( $this->sshot_query_hosts ) - 1 );
		$this->sshot_query_hostname = $this->sshot_query_hosts [$rand];
	}
	
	public function Execute($cmd, $params, $post_params = null, $hostname, $port = null, $return_array = false) {
		
		$cmd = urlencode ( $cmd );
		if (! $port)
			$port = $this->port;
		else
			$port = ( int ) $port;
		
		if ($params != '') {
			array_map ( urlencode, $params );
			$query = implode ( '/', $params );
			$url = "http://$hostname:$port/$this->key/$cmd/" . $query . '/';
		} else {
			$url = "http://$hostname:$port/$this->key/$cmd";
		}
		
		if ($post_params)
			$ret = parent::PostURL ( $url, $post_params );
		else
			$ret = parent::GetURL ( $url );
		
		return json_decode ( $ret, $return_array );
	}
	
	public function OppFinderFetchKeywords($main_id) {
		
		if (! $main_id)
			return 'FAIL';
		
		return $this->Execute ( 'opp_finder_fetch_keywords.py', array (( int ) $main_id ), null, $this->opp_finder_hostname );
	}
	
	public function OppFinderFetchMaps($main_id) {
		
		if (! $main_id)
			return 'FAIL';
		
		return $this->Execute ( 'opp_finder_fetch_maps.py', array (( int ) $main_id ), null, $this->opp_finder_hostname );
	}
	
	public function OppFinderFetchQuestions($cat_id) {
		
		if (! $cat_id)
			return 'FAIL';
		
		return $this->Execute ( 'opp_finder_fetch_questions.py', array (( int ) $cat_id ), null, $this->opp_finder_hostname );
	}
	
	public function OppFinderFetchSearchInfo($main_id) {
		if (! $main_id)
			return 'FAIL';
		
		return $this->Execute ( 'opp_finder_fetch_search_info.py', array (( int ) $main_id ), null, $this->opp_finder_hostname );
	}
	
	public function PublishToMessageQueue($task, $data) {
		
		if (! $task || ! $data)
			return 'FAIL';
		
		return $this->Execute ( 'publish_to_mqueue', array ($task ), array ('data' => json_encode ( $data ) ), $this->mqueue_hostname );
	}
	
	public function GetGoogleAdsImageURL($main_id) {
		
		if (! $main_id)
			return 'FAIL';
		
		return $this->Execute ( 'fetch_google_ads_image.py', array (( int ) $main_id ), null, $this->opp_finder_hostname );
	
	}
	
	public function GetCouponImageURL($coupon_html) {
		
		return $this->Execute ( 'fetch_coupon_image.py', '', array ('html' => $coupon_html ), $this->coupon_creator_hostname );
	}
	
	public function GetUserSFTier($email) {
		
		if ($email == '')
			return '';
		
		$tier = $this->Execute ( 'salesforce_query.py', '', array ('email' => $email, 'command' => 'subscriber_tier' ), $this->sf_query_hostname );
		
		if ($tier == 'ERR_NO_DATA' || $tier == 'ERR_INVALID_COMMAND')
			return '';
		else
			return $tier;
	
	}
	
	public function getScreenCaptureImageURL($url) {
		
		return $this->Execute ( 'website_screenshot.py', '', array ('website_url' => $url ), $this->sshot_query_hostname );
	}
	
	public function GetSocialInfo($email) {
		return $this->Execute ( 'get_social_info.py', '', array ('email' => $email ), $this->si_query_hostname, null, true );
	
	}
}
?>
